package com.xbongbong.paas.help.subform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.help.subform.abstracts.AbstractUserTeamTransferSubForm;
import com.xbongbong.saas.domain.entity.PayPlanSheetUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.subform.PayPlanSheetTeamEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 付款单团队es和数据库格式转化
 * @author 魏荣杰
 * @date 2020/10/13 15:16
 * @since
 * @version
 */
@Component
public class PayPlanSheetTeamTransferSubForm extends AbstractUserTeamTransferSubForm {

    private static final Logger LOG = LoggerFactory.getLogger(PayPlanSheetTeamTransferSubForm.class);

    @Override
    public PaasFormDataEntity transferTeamToFormData(UserTeamEntity userTeamEntity) {
        PayPlanSheetUserEntity payPlanSheetUserEntity = (PayPlanSheetUserEntity) userTeamEntity;
        PaasFormDataEntity paasFormDataEntity = createPaasFormDataEntityForUserTeam(userTeamEntity);

        JSONObject teamJson = JSONObject.parseObject(JSON.toJSONString(payPlanSheetUserEntity));

        JSONObject data = new JSONObject();

        PayPlanSheetTeamEnum[] payPlanSheetTeamEnums = PayPlanSheetTeamEnum.values();
        for (PayPlanSheetTeamEnum payPlanSheetTeamEnum : payPlanSheetTeamEnums) {
            String dbAttr = payPlanSheetTeamEnum.getDbAttr();
            String dataAttr = payPlanSheetTeamEnum.getDataAttr();
            Integer fieldType = payPlanSheetTeamEnum.getFieldType();
            formatDataForEs(teamJson, data, dbAttr, dataAttr, fieldType);
        }
        paasFormDataEntity.setData(data);

        return paasFormDataEntity;
    }

    @Override
    public UserTeamEntity transferFormDataToTeam(PaasFormDataEntity paasFormDataEntity) {
        JSONObject data = paasFormDataEntity.getData();

        JSONObject dbData = new JSONObject();
        PayPlanSheetTeamEnum[] payPlanSheetTeamEnums = PayPlanSheetTeamEnum.values();
        for (PayPlanSheetTeamEnum payPlanSheetTeamEnum : payPlanSheetTeamEnums) {
            String dbAttr = payPlanSheetTeamEnum.getDbAttr();
            String dataAttr = payPlanSheetTeamEnum.getDataAttr();
            Integer fieldType = payPlanSheetTeamEnum.getFieldType();
            formatDataForDb(data, dbData, dbAttr, dataAttr, fieldType);

        }
        PayPlanSheetUserEntity payPlanSheetUserEntity = JSON.parseObject(dbData.toJSONString(), PayPlanSheetUserEntity.class);
        payPlanSheetUserEntity.setAddTime(paasFormDataEntity.getAddTime());
        payPlanSheetUserEntity.setUpdateTime(paasFormDataEntity.getUpdateTime());
        payPlanSheetUserEntity.setCorpid(paasFormDataEntity.getCorpid());
        payPlanSheetUserEntity.setDel(paasFormDataEntity.getDel());
        payPlanSheetUserEntity.setId(paasFormDataEntity.getId());

        return payPlanSheetUserEntity;
    }
}
